package com.example.englishspeechproject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import entity.Question;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button btnStart;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        String destPath="data/data/"+getPackageName()+"/databases/english_speech_db";
        File file=new File(destPath);
        if(!file.exists()){
        	try {
				CopyDB(getBaseContext().getAssets().open("english_speech_db"), new FileOutputStream(destPath));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        btnStart=(Button) findViewById(R.id.btn_start);
        /*DBAdapter adapter=new DBAdapter(this);
        adapter.open();
        Question question=adapter.getQuestionById(1);
        Toast.makeText(this, question.getId()+"\n"+question.getValidAnswerMedia(), Toast.LENGTH_LONG).show();
        adapter.close();*/
        btnStart.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent("Question"));
			}
		});
    }
    
    public void CopyDB(InputStream inputStream,OutputStream outputStream) throws IOException{
    	byte[] buffer=new byte[1024];
    	int length;
    	while((length=inputStream.read(buffer))>0){
    		outputStream.write(buffer, 0, length);
    	}
    	inputStream.close();
    	outputStream.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}
